<template>
	<view>
		<u-navbar
			title="选择优惠券"
			:autoBack="true"
			placeholder
		>
		</u-navbar>
		<scroll-view @scrolltolower="onLower" :scroll-y="true" :style="{height: scrollViewHeight}">
			<template v-if="loading">
				<view style="padding: 32rpx;">
					<u-skeleton :avatarSize="0" style="margin-bottom: 32rpx;" :loading="loading" avatar rows="3" v-for="i in 8" :key="i"></u-skeleton>
				</view>
			</template>
			<template v-else-if="!loading && list.length === 0">
				<u-empty
				    mode="coupon"
					icon="http://cdn.uviewui.com/uview/empty/coupon.png"
				>
				</u-empty>
			</template>
			<template v-else-if="!loading && list.length > 0">
				<view style="padding: 32rpx;">
					<u-radio-group
						placement="column"
						v-model="couponId">
						<view class="coupon-item" @click="chooseCoupon(item)"  v-for="item in list" :key="item.id">
							<text class="no-use" v-if="!item.allowUse">不可用</text>
							<view class="coupon-item-l-box">
								<view class="coupon-item-l">
									<view class="coupon-item-l-tag">{{item.category_dictText}}</view>
									<view class="coupon-item-l-content">
										<text :style="{color: appColor, fontSize: '48rpx',fontWeight: 'bold'}">&yen;{{item.discount}}</text>
										<text :style="{color: appColor, fontSize: '26rpx'}">满{{item.lowCashPrice}}元可用</text>
									</view>
								</view>
								<view class="coupon-item-m">
									<text style="font-size: 32rpx;color: #333; font-weight: bold;">{{item.couponName}}</text>
									<view style="height: 32rpx;"/>
									<text style="font-size: 26rpx;color: #666; font-weight: bold;" v-if="item.expiryType === '1'">{{Number(item.expiryDay) === 0 ? '长期有效' : `领取后${item.expiryDay}日内有效`}}</text>
									<text style="font-size: 26rpx;color: #666; font-weight: bold;" v-else>{{item.startTime}}至{{item.endTime}}有效</text>
								</view>
							</view>
							<view class="coupon-item-r">
								<u-radio :disabled="!item.allowUse" :name="item.id" :activeColor="appColor"></u-radio>
							</view>
						</view>
					</u-radio-group>
					<u-loadmore :status="status" />
				</view>
			</template>
		</scroll-view>
		<view class="good-detail-bootom" v-if="!loading">
			<view style="margin: 0 32rpx;height: 100%;width: calc(100vw - 64rpx);display: flex;align-items: center;justify-content: space-between;">
				<view class="good-detail-bootom-l1">
					<view class="good-detail-bootom-l1-t">
						<text>已选{{counponPrice > 0 ? 1 : 0}}张, 可减: </text>
						<text style="padding-left: 8rpx;color: #f40215;font-size: 36rpx;">&yen;{{counponPrice}}</text>
					</view>
				</view>	
				<view class="good-detail-bootom-r">
					<u-button @click="toPay" :color="appColor" text="确定" shape="circle"></u-button>
				</view>
			</view>	
		</view>
	</view>
</template>

<script>
	import {getDictItems, getAllowUseList, getGoodsAllowUseList, getTicketAllowUseList} from '@/api/me.js';
	import {mapActions, mapState} from 'vuex';
	export default {
		data() {
			return {
				couponId: '-1',
				pageSize: 8,
				pageNo: 1,
				list: [], // 优惠券列表
				loading: true,
				status: 'loadmore',
				adoptId: '',
				buyCount: '',
				way: 0,
				ticketId: '',
				play_time: ''
			}
		},
		computed: {
			...mapState(['cartDetail']),
			appColor() {
				return this.appPrimaryColor
			},
			scrollViewHeight() {
				return this.scrollHeight - 44 - 50 + 'px';
			},
			counponPrice() {
				if(this.couponId === '-1' || this.list.length === 0) {
					return 0
				}
				return this.list.find(item => item.id === this.couponId).discount
			}
		},
		onLoad({couponId, adoptId, buyCount, way, ticketId, time}) {
			this.way = way || 0
			this.couponId = couponId;
			this.adoptId = adoptId;
			this.ticketId = ticketId;
			this.buyCount = buyCount;
			this.play_time = time;
			this.getList();
		},
		methods: {
			...mapActions(['setCurCoupon']),
			toPay() {
				if(this.couponId === '-1') {
					this.setCurCoupon({curCoupon: {}});
					uni.navigateBack({
						delta: 1
					})
				} else {
					this.setCurCoupon({curCoupon: this.list.find(item => item.id === this.couponId)});
					uni.navigateBack({
						delta: 1
					})
				}
			},
			// 选择券
			chooseCoupon({id, allowUse}) {
				// 当前券不能使用
				if(!allowUse) {
					return
				}
				if(this.couponId == id) {
					this.couponId = '-1'
				} else {
					this.couponId = id;
				}
			},
			// 优惠券列表
			getList() {
				if(this.pageNo === 1) {
					this.loading = true;
				}
				let postObj = {};
				// 认养
				if(Number(this.way) === 0) {
					postObj = {
						pageNo: this.pageNo,
						pageSize: this.pageSize,
						buyCount: this.buyCount,
						adoptId: this.adoptId,
					}
					getAllowUseList(postObj).then(res => {
						const {current, pages, records} = res.result;
						if(this.pageNo === 1) {
							let empty = [];
							this.list = empty.concat(records);
						} else {
							this.list = this.list.concat(records);
						}
						if(current >= pages) {
							this.status = 'nomore'
						} else {
							this.pageNo ++;
							this.status = 'loading'
						}
					}).finally(() => {
						this.loading = false;
					})
				// 商城	
				} else if(Number(this.way) === 1) {
					let detailList = [];
					this.cartDetail.forEach(item => {
						const {buyCount, goodsId, skuId} = item;
						detailList.push({
							buyCount,
							goodsId,
						})
					})
					postObj = {
						pageNo: this.pageNo,
						pageSize: this.pageSize,
						detailList
					}
					getGoodsAllowUseList(postObj).then(res => {
						const {current, pages, records} = res.result;
						if(this.pageNo === 1) {
							let empty = [];
							this.list = empty.concat(records);
						} else {
							this.list = this.list.concat(records);
						}
						this.pageNo ++;
						if(current >= pages) {
							this.status = 'nomore'
						} else {
							this.status = 'loading'
						}
					}).finally(() => {
						this.loading = false;
					})
				// 门票	
				} else if(Number(this.way) === 2) {
					postObj = {
						pageNo: this.pageNo,
						pageSize: this.pageSize,
						ticketId: this.ticketId,
						buyCount: this.buyCount,
						ticketTime: this.play_time
					}
					getTicketAllowUseList(postObj).then(res => {
						const {current, pages, records} = res.result;
						if(this.pageNo === 1) {
							let empty = [];
							this.list = empty.concat(records);
						} else {
							this.list = this.list.concat(records);
						}
						this.pageNo ++;
						if(current >= pages) {
							this.status = 'nomore'
						} else {
							this.status = 'loading'
						}
					}).finally(() => {
						this.loading = false;
					})
				}
			},
			onLower() {
				if(this.status === 'loading') {
					this.getList();
				}
			}
		}
	}
</script>

<style>
	page {
		background-color: #f5f5f5;
	}
</style>
<style lang="scss" scoped>
	.good-detail-bootom {
		background-color: #ffffff;
		position: fixed;
		height: 50px;
		width: 100vw;
		bottom: 0;
		left: 0;
		padding-bottom: constant(safe-area-inset-bottom);     /*兼容 iOS<11.2*/
		padding-bottom: env(safe-area-inset-bottom);  /*兼容 iOS>11.2*/
		box-sizing: content-box;
		.good-detail-bootom-l1 {
			display: flex;
			flex-direction: column;
			justify-content: center;
			.good-detail-bootom-l1-t {
				font-size: 28rpx;
			}
		}
		.good-detail-bootom-r {
			width: 320rpx;
			text-align: right
		}
	}
	.coupon-item {
		border-radius: 16rpx;
		width: 100%;
		height: 200rpx;
		-webkit-mask-image: radial-gradient(circle at right 54px bottom 8px, transparent 8px, red 8.5px), radial-gradient(closest-side circle at 50%, red 99%, transparent 100%);
		-webkit-mask-size: 100%, 4px 12px;
		-webkit-mask-repeat: repeat, repeat-y;
		-webkit-mask-position: 0 8px, calc(100% - 52px);
		-webkit-mask-composite: source-out;
		mask-composite: subtract;
		background-color: #fff;
		position: relative;
		overflow: hidden;
		margin-bottom: 32rpx;
		.no-use {
			font-size: 38rpx;
			color: #999;
			position: absolute;
			left: 74%;
			top: 50%;
			transform: translate(-50%, -50%) rotate(-45deg);
		}
		.coupon-item-l-box {
			padding-top: 16rpx;
			height: calc(100% - 32rpx);
			width: calc(100% - 52px);
			display: flex;
			.coupon-item-l {
				width: 200rpx;
				height: 100%;
				border-right: 1rpx dashed #f2f2f2;
				position: relative;
				.coupon-item-l-tag {
					left: 0;
					top: -16rpx;
					position: absolute;
					height: 36rpx;
					line-height: 36rpx;
					padding: 4rpx 8rpx;
					border-radius: 0 0 16rpx 0;
					background-color: red;
					color: #fff;
					font-size: 24rpx;
				}
				.coupon-item-l-content {
					width: 200rpx;
					height: 100%;
					display: flex;
					align-items: center;
					justify-content: center;
					flex-direction: column;
				}
			}
			.coupon-item-m {
				width: calc(100% - 200rpx);
				height: 100%;
				display: flex;
				flex-direction: column;
				justify-content: center;
				padding: 0 16rpx;
			}
		}
		.coupon-item-r {
			position: absolute;
			width: 52px;
			height: 100%;
			right: 0;
			top: 0;
			text-align: center;
			font-size: 32rpx;
			color: #fff;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
		}
	}
</style>